{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Use Rootly with agents.json\n",
    "This notebook demonstrates how to use agents.json to interact with the Rootly API. This simple example shows loading the agents.json file and using it to execute Rootly tasks.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Package                   Version     Editable project location\n",
      "------------------------- ----------- -------------------------------------------------------\n",
      "agentsjson                0.1.1       /Users/yagnyapatel/Projects/Wildcard/agents-json/python\n",
      "aiohappyeyeballs          2.4.4\n",
      "aiohttp                   3.11.11\n",
      "aiosignal                 1.3.2\n",
      "alembic                   1.14.1\n",
      "annotated-types           0.7.0\n",
      "anyio                     4.8.0\n",
      "appnope                   0.1.4\n",
      "asttokens                 3.0.0\n",
      "asyncpg                   0.30.0\n",
      "attrs                     25.1.0\n",
      "babel                     2.17.0\n",
      "bcrypt                    4.2.1\n",
      "cachetools                5.5.1\n",
      "certifi                   2024.12.14\n",
      "cffi                      1.17.1\n",
      "charset-normalizer        3.4.1\n",
      "click                     8.1.8\n",
      "comm                      0.2.2\n",
      "cryptography              44.0.0\n",
      "curlify                   2.2.1\n",
      "debugpy                   1.8.12\n",
      "decorator                 5.1.1\n",
      "Delorean                  1.0.0\n",
      "deprecation               2.1.0\n",
      "distro                    1.9.0\n",
      "ecdsa                     0.19.0\n",
      "executing                 2.2.0\n",
      "facebook_business         21.0.5\n",
      "fastapi                   0.115.8\n",
      "frozenlist                1.5.0\n",
      "google-api-core           2.24.1\n",
      "google-api-python-client  2.160.0\n",
      "google-auth               2.38.0\n",
      "google-auth-httplib2      0.2.0\n",
      "google-auth-oauthlib      1.2.1\n",
      "googleapis-common-protos  1.66.0\n",
      "gotrue                    2.11.3\n",
      "greenlet                  3.1.1\n",
      "grpcio                    1.70.0\n",
      "grpcio-tools              1.70.0\n",
      "h11                       0.14.0\n",
      "h2                        4.1.0\n",
      "hpack                     4.1.0\n",
      "httpcore                  1.0.7\n",
      "httplib2                  0.22.0\n",
      "httpx                     0.28.1\n",
      "hubspot                   0.1.14.dev0\n",
      "hubspot-api-client        11.1.0\n",
      "humanize                  4.11.0\n",
      "hyperframe                6.1.0\n",
      "idna                      3.10\n",
      "ipykernel                 6.29.5\n",
      "ipython                   8.31.0\n",
      "jedi                      0.19.2\n",
      "Jinja2                    3.1.5\n",
      "jiter                     0.8.2\n",
      "jsonschema                4.23.0\n",
      "jsonschema-specifications 2024.10.1\n",
      "jupyter_client            8.6.3\n",
      "jupyter_core              5.7.2\n",
      "Mako                      1.3.8\n",
      "markdown-it-py            3.0.0\n",
      "MarkupSafe                3.0.2\n",
      "matplotlib-inline         0.1.7\n",
      "mdurl                     0.1.2\n",
      "multidict                 6.1.0\n",
      "nest-asyncio              1.6.0\n",
      "nodeenv                   1.9.1\n",
      "numpy                     2.2.2\n",
      "oauthlib                  3.2.2\n",
      "openai                    1.61.0\n",
      "packaging                 24.2\n",
      "parso                     0.8.4\n",
      "passlib                   1.7.4\n",
      "pexpect                   4.9.0\n",
      "pip                       24.3.1\n",
      "platformdirs              4.3.6\n",
      "portalocker               2.10.1\n",
      "postgrest                 0.19.3\n",
      "prisma                    0.15.0\n",
      "prompt_toolkit            3.0.50\n",
      "propcache                 0.2.1\n",
      "proto-plus                1.26.0\n",
      "protobuf                  5.29.3\n",
      "psutil                    6.1.1\n",
      "psycopg2                  2.9.10\n",
      "psycopg2-binary           2.9.10\n",
      "ptyprocess                0.7.0\n",
      "pure_eval                 0.2.3\n",
      "pyasn1                    0.6.1\n",
      "pyasn1_modules            0.4.1\n",
      "pycountry                 24.6.1\n",
      "pycparser                 2.22\n",
      "pydantic                  2.10.6\n",
      "pydantic_core             2.27.2\n",
      "Pygments                  2.19.1\n",
      "PyJWT                     2.10.0\n",
      "Pympler                   1.1\n",
      "pyparsing                 3.2.1\n",
      "python-benedict           0.33.2\n",
      "python-dateutil           2.9.0.post0\n",
      "python-dotenv             1.0.1\n",
      "python-fsutil             0.14.1\n",
      "python-jose               3.3.0\n",
      "python-multipart          0.0.20\n",
      "python-slugify            8.0.4\n",
      "pytz                      2025.1\n",
      "PyYAML                    6.0.2\n",
      "pyzmq                     26.2.1\n",
      "qdrant-client             1.13.2\n",
      "realtime                  2.3.0\n",
      "referencing               0.36.2\n",
      "requests                  2.32.2\n",
      "requests-oauthlib         2.0.0\n",
      "rich                      13.9.4\n",
      "rpds-py                   0.22.3\n",
      "rsa                       4.9\n",
      "setuptools                75.8.0\n",
      "simplejson                3.19.3\n",
      "six                       1.17.0\n",
      "sniffio                   1.3.1\n",
      "SQLAlchemy                2.0.37\n",
      "stack-data                0.6.3\n",
      "starlette                 0.41.3\n",
      "storage3                  0.11.3\n",
      "StrEnum                   0.4.15\n",
      "stripe                    7.11.0\n",
      "supabase                  2.11.0\n",
      "supafunc                  0.9.3\n",
      "text-unidecode            1.3\n",
      "tomlkit                   0.13.2\n",
      "tornado                   6.4.2\n",
      "tqdm                      4.67.1\n",
      "traitlets                 5.14.3\n",
      "tweepy                    4.15.0\n",
      "typing_extensions         4.12.2\n",
      "tzlocal                   5.3\n",
      "uritemplate               4.1.1\n",
      "urllib3                   2.3.0\n",
      "uvicorn                   0.34.0\n",
      "wcwidth                   0.2.13\n",
      "websockets                14.2\n",
      "wildcard-gateway          0.1.0       /Users/yagnyapatel/Projects/Wildcard\n",
      "yarl                      1.18.3\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: agentsjson in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (0.1.1)\n",
      "Requirement already satisfied: google-api-python-client<3.0.0,>=2.0.0 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from agentsjson) (2.160.0)\n",
      "Requirement already satisfied: pydantic<3.0.0,>=2.0.0 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from agentsjson) (2.10.6)\n",
      "Requirement already satisfied: pympler<2.0,>=1.1 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from agentsjson) (1.1)\n",
      "Requirement already satisfied: python-benedict<0.34.0,>=0.33.0 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from agentsjson) (0.33.2)\n",
      "Requirement already satisfied: pyyaml<7.0.0,>=6.0.0 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from agentsjson) (6.0.2)\n",
      "Requirement already satisfied: requests<3.0.0,>=2.0.0 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from agentsjson) (2.32.2)\n",
      "Requirement already satisfied: six<2.0.0,>=1.16.0 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from agentsjson) (1.17.0)\n",
      "Requirement already satisfied: stripe==7.11.0 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from agentsjson) (7.11.0)\n",
      "Requirement already satisfied: tweepy<5.0.0,>=4.0.0 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from agentsjson) (4.15.0)\n",
      "Requirement already satisfied: typing-extensions<5.0.0,>=4.0.0 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from agentsjson) (4.12.2)\n",
      "Requirement already satisfied: httplib2<1.dev0,>=0.19.0 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from google-api-python-client<3.0.0,>=2.0.0->agentsjson) (0.22.0)\n",
      "Requirement already satisfied: google-auth!=2.24.0,!=2.25.0,<3.0.0.dev0,>=1.32.0 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from google-api-python-client<3.0.0,>=2.0.0->agentsjson) (2.38.0)\n",
      "Requirement already satisfied: google-auth-httplib2<1.0.0,>=0.2.0 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from google-api-python-client<3.0.0,>=2.0.0->agentsjson) (0.2.0)\n",
      "Requirement already satisfied: google-api-core!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.0,<3.0.0.dev0,>=1.31.5 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from google-api-python-client<3.0.0,>=2.0.0->agentsjson) (2.24.1)\n",
      "Requirement already satisfied: uritemplate<5,>=3.0.1 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from google-api-python-client<3.0.0,>=2.0.0->agentsjson) (4.1.1)\n",
      "Requirement already satisfied: annotated-types>=0.6.0 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from pydantic<3.0.0,>=2.0.0->agentsjson) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.27.2 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from pydantic<3.0.0,>=2.0.0->agentsjson) (2.27.2)\n",
      "Requirement already satisfied: python-fsutil<1.0.0,>=0.9.3 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from python-benedict<0.34.0,>=0.33.0->agentsjson) (0.14.1)\n",
      "Requirement already satisfied: python-slugify<9.0.0,>=7.0.0 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from python-benedict<0.34.0,>=0.33.0->agentsjson) (8.0.4)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from requests<3.0.0,>=2.0.0->agentsjson) (3.4.1)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from requests<3.0.0,>=2.0.0->agentsjson) (3.10)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from requests<3.0.0,>=2.0.0->agentsjson) (2.3.0)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from requests<3.0.0,>=2.0.0->agentsjson) (2024.12.14)\n",
      "Requirement already satisfied: oauthlib<4,>=3.2.0 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from tweepy<5.0.0,>=4.0.0->agentsjson) (3.2.2)\n",
      "Requirement already satisfied: requests-oauthlib<3,>=1.2.0 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from tweepy<5.0.0,>=4.0.0->agentsjson) (2.0.0)\n",
      "Requirement already satisfied: googleapis-common-protos<2.0.dev0,>=1.56.2 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from google-api-core!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.0,<3.0.0.dev0,>=1.31.5->google-api-python-client<3.0.0,>=2.0.0->agentsjson) (1.66.0)\n",
      "Requirement already satisfied: protobuf!=3.20.0,!=3.20.1,!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<6.0.0.dev0,>=3.19.5 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from google-api-core!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.0,<3.0.0.dev0,>=1.31.5->google-api-python-client<3.0.0,>=2.0.0->agentsjson) (5.29.3)\n",
      "Requirement already satisfied: proto-plus<2.0.0dev,>=1.22.3 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from google-api-core!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.0,<3.0.0.dev0,>=1.31.5->google-api-python-client<3.0.0,>=2.0.0->agentsjson) (1.26.0)\n",
      "Requirement already satisfied: cachetools<6.0,>=2.0.0 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from google-auth!=2.24.0,!=2.25.0,<3.0.0.dev0,>=1.32.0->google-api-python-client<3.0.0,>=2.0.0->agentsjson) (5.5.1)\n",
      "Requirement already satisfied: pyasn1-modules>=0.2.1 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from google-auth!=2.24.0,!=2.25.0,<3.0.0.dev0,>=1.32.0->google-api-python-client<3.0.0,>=2.0.0->agentsjson) (0.4.1)\n",
      "Requirement already satisfied: rsa<5,>=3.1.4 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from google-auth!=2.24.0,!=2.25.0,<3.0.0.dev0,>=1.32.0->google-api-python-client<3.0.0,>=2.0.0->agentsjson) (4.9)\n",
      "Requirement already satisfied: pyparsing!=3.0.0,!=3.0.1,!=3.0.2,!=3.0.3,<4,>=2.4.2 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from httplib2<1.dev0,>=0.19.0->google-api-python-client<3.0.0,>=2.0.0->agentsjson) (3.2.1)\n",
      "Requirement already satisfied: text-unidecode>=1.3 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from python-slugify<9.0.0,>=7.0.0->python-benedict<0.34.0,>=0.33.0->agentsjson) (1.3)\n",
      "Requirement already satisfied: pyasn1<0.7.0,>=0.4.6 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from pyasn1-modules>=0.2.1->google-auth!=2.24.0,!=2.25.0,<3.0.0.dev0,>=1.32.0->google-api-python-client<3.0.0,>=2.0.0->agentsjson) (0.6.1)\n",
      "\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.3.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m25.0\u001b[0m\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n",
      "Note: you may need to restart the kernel to use updated packages.\n",
      "Requirement already satisfied: openai in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (1.61.0)\n",
      "Requirement already satisfied: anyio<5,>=3.5.0 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from openai) (4.8.0)\n",
      "Requirement already satisfied: distro<2,>=1.7.0 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from openai) (1.9.0)\n",
      "Requirement already satisfied: httpx<1,>=0.23.0 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from openai) (0.28.1)\n",
      "Requirement already satisfied: jiter<1,>=0.4.0 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from openai) (0.8.2)\n",
      "Requirement already satisfied: pydantic<3,>=1.9.0 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from openai) (2.10.6)\n",
      "Requirement already satisfied: sniffio in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from openai) (1.3.1)\n",
      "Requirement already satisfied: tqdm>4 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from openai) (4.67.1)\n",
      "Requirement already satisfied: typing-extensions<5,>=4.11 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from openai) (4.12.2)\n",
      "Requirement already satisfied: idna>=2.8 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from anyio<5,>=3.5.0->openai) (3.10)\n",
      "Requirement already satisfied: certifi in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from httpx<1,>=0.23.0->openai) (2024.12.14)\n",
      "Requirement already satisfied: httpcore==1.* in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from httpx<1,>=0.23.0->openai) (1.0.7)\n",
      "Requirement already satisfied: h11<0.15,>=0.13 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai) (0.14.0)\n",
      "Requirement already satisfied: annotated-types>=0.6.0 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from pydantic<3,>=1.9.0->openai) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.27.2 in /Users/yagnyapatel/Projects/Wildcard/agents-json/.venv/lib/python3.13/site-packages (from pydantic<3,>=1.9.0->openai) (2.27.2)\n",
      "\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.3.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m25.0\u001b[0m\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "%pip install agentsjson\n",
    "%pip install openai"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Setup API keys and references to `agents.json` files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "ROOTLY_API_KEY=\"your_rootly_api_key\"\n",
    "OPENAI_API_KEY=\"your_openai_api_key\"\n",
    "agents_json_url = \"https://gist.githubusercontent.com/yagnyaPatel/f21f8c0a6c221213584bbb7a99a0e1dc/raw/ec1cc50a243a1e63c161fc3b9a27b6302dac252b/agents.json\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load the agents.json file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "from agentsjson.core.models import Flow\n",
    "from agentsjson.core.models.bundle import Bundle\n",
    "import agentsjson.core as core\n",
    "\n",
    "# load the agents.json file\n",
    "data: Bundle = core.load_agents_json(agents_json_url)\n",
    "flows = data.agentsJson.flows"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Define the Agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "query = \"Get me the incident details on the monkey in the server room\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "from agentsjson.core import ToolFormat\n",
    "\n",
    "# Format the flows data for the prompt\n",
    "flows_context = core.flows_prompt(flows)\n",
    "\n",
    "# Create the system prompt\n",
    "system_prompt = f\"\"\"You are an AI assistant that helps users interact with the Rootly API.\n",
    "You have access to the following API flows:\n",
    "\n",
    "{flows_context}\n",
    "\n",
    "Analyze the user's request and use the appropriate API flows to accomplish the task.\n",
    "You must give your arguments for the tool call as Structued Outputs JSON with keys `parameters` and `requestBody`\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Configure Auth"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "from agentsjson.core.models.auth import AuthType, BearerAuthConfig\n",
    "auth = BearerAuthConfig(type=AuthType.BEARER, token=ROOTLY_API_KEY)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Run the agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'retrieve_top_incident_details': {'success': {'data': {'id': '41b5b7f4-db7d-4a69-a44f-13c34ab20af9', 'type': 'incidents', 'attributes': {'parent_incident_id': None, 'sequential_id': 3, 'title': 'Prod Down', 'slug': 'prod-down', 'kind': 'normal', 'private': False, 'summary': 'The monkey got into the server room and took prod down', 'status': 'started', 'source': 'web', 'url': 'https://rootly.com/account/incidents/3-prod-down', 'short_url': 'https://root.ly/rjaxdj', 'user': {'data': {'id': '86651', 'type': 'users', 'attributes': {'name': 'Hunly AI', 'email': 'contact@hunly.io', 'phone': None, 'phone_2': None, 'full_name': 'Hunly AI', 'full_name_with_team': '[Hunly] Hunly AI', 'slack_id': None, 'time_zone': 'America/Los_Angeles', 'updated_at': '2025-02-13T15:32:22.429-08:00', 'created_at': '2025-02-13T02:34:09.838-08:00'}, 'relationships': {'email_addresses': {'data': [{'id': '705e87e1-e7b9-4e3c-ade3-387c353ae5f2', 'type': 'user_email_addresses'}]}, 'phone_numbers': {'data': []}, 'devices': {'data': []}}}}, 'severity': {'data': {'id': 'b3430b87-46f7-4596-98cc-b853cd7db062', 'type': 'severities', 'attributes': {'name': 'SEV0', 'slug': 'sev0', 'description': \"Critical system issue actively impacting many customers' ability to use the product (e.g. website outage)\", 'severity': 'critical', 'color': '#C73C40', 'position': 1, 'notify_emails': [], 'slack_channels': [], 'slack_aliases': [], 'created_at': '2025-02-13T02:34:31.498-08:00', 'updated_at': '2025-02-13T02:34:31.498-08:00'}}}, 'in_triage_by': None, 'started_by': {'data': {'id': '86651', 'type': 'users', 'attributes': {'name': 'Hunly AI', 'email': 'contact@hunly.io', 'phone': None, 'phone_2': None, 'full_name': 'Hunly AI', 'full_name_with_team': '[Hunly] Hunly AI', 'slack_id': None, 'time_zone': 'America/Los_Angeles', 'updated_at': '2025-02-13T15:32:22.429-08:00', 'created_at': '2025-02-13T02:34:09.838-08:00'}, 'relationships': {'email_addresses': {'data': [{'id': '705e87e1-e7b9-4e3c-ade3-387c353ae5f2', 'type': 'user_email_addresses'}]}, 'phone_numbers': {'data': []}, 'devices': {'data': []}}}}, 'mitigated_by': None, 'resolved_by': None, 'closed_by': None, 'cancelled_by': None, 'mitigation_message': None, 'resolution_message': None, 'cancellation_message': None, 'public_title': None, 'duplicate_incident_id': None, 'retrospective_progress_status': 'not_started', 'zoom_meeting_id': None, 'zoom_meeting_start_url': None, 'zoom_meeting_join_url': None, 'zoom_meeting_password': None, 'zoom_meeting_pstn_password': None, 'zoom_meeting_h323_password': None, 'zoom_meeting_global_dial_in_numbers': [], 'shortcut_story_id': None, 'shortcut_story_url': None, 'shortcut_task_id': None, 'shortcut_task_url': None, 'asana_task_id': None, 'asana_task_url': None, 'github_issue_id': None, 'github_issue_url': None, 'gitlab_issue_id': None, 'gitlab_issue_url': None, 'jira_issue_key': None, 'jira_issue_id': None, 'jira_issue_url': None, 'google_meeting_id': None, 'google_meeting_url': None, 'trello_card_id': None, 'trello_card_url': None, 'linear_issue_id': None, 'linear_issue_url': None, 'zendesk_ticket_id': None, 'zendesk_ticket_url': None, 'motion_task_id': None, 'motion_task_url': None, 'clickup_task_id': None, 'clickup_task_url': None, 'slack_channel_name': None, 'slack_channel_id': None, 'slack_channel_url': None, 'slack_channel_short_url': None, 'slack_channel_deep_link': None, 'slack_channel_archived': False, 'service_now_incident_id': None, 'service_now_incident_key': None, 'service_now_incident_url': None, 'opsgenie_incident_id': None, 'opsgenie_incident_url': None, 'opsgenie_alert_id': None, 'opsgenie_alert_url': None, 'victor_ops_incident_id': None, 'victor_ops_incident_url': None, 'pagerduty_incident_id': None, 'pagerduty_incident_number': None, 'pagerduty_incident_url': None, 'mattermost_channel_id': None, 'mattermost_channel_name': None, 'mattermost_channel_url': None, 'confluence_page_id': None, 'confluence_page_url': None, 'quip_page_id': None, 'quip_page_url': None, 'airtable_base_key': None, 'airtable_table_name': None, 'airtable_record_id': None, 'airtable_record_url': None, 'google_drive_id': None, 'google_drive_parent_id': None, 'google_drive_url': None, 'sharepoint_page_id': None, 'sharepoint_page_url': None, 'datadog_notebook_id': None, 'datadog_notebook_url': None, 'freshservice_ticket_id': None, 'freshservice_ticket_url': None, 'freshservice_task_id': None, 'freshservice_task_url': None, 'scheduled_for': None, 'scheduled_until': None, 'in_triage_at': None, 'started_at': '2025-02-13T02:35:27.704-08:00', 'detected_at': None, 'acknowledged_at': None, 'mitigated_at': None, 'resolved_at': None, 'closed_at': None, 'cancelled_at': None, 'created_at': '2025-02-13T02:35:27.741-08:00', 'updated_at': '2025-02-13T02:35:28.123-08:00', 'labels': {}}, 'relationships': {'causes': {'data': []}, 'subscribers': {'data': [{'id': '325086f8-4139-4124-a1a2-eb567ba57b59', 'type': 'incident_subscribers'}]}, 'roles': {'data': [{'id': '2fd40e32-67d3-42a2-8725-860454080bb3', 'type': 'incident_role_assignments'}, {'id': '093f8d53-b2bd-4d1f-a6fc-51cc4fe35df3', 'type': 'incident_role_assignments'}, {'id': 'f0ddeacf-93bd-4330-adf8-8b3f7b30162a', 'type': 'incident_role_assignments'}, {'id': 'f5a58b47-6da1-408f-845a-cd1dc74dcfc8', 'type': 'incident_role_assignments'}]}, 'environments': {'data': []}, 'incident_types': {'data': [{'id': 'c933dc55-fedb-4d0f-a84a-7234a64c2f03', 'type': 'incident_types'}, {'id': '183a35ec-1956-4454-b58e-2b47bdf5ae29', 'type': 'incident_types'}]}, 'services': {'data': []}, 'functionalities': {'data': []}, 'groups': {'data': []}, 'events': {'data': [{'id': '56d562ce-d953-4b54-98a0-3af53f8def5b', 'type': 'incident_events'}, {'id': '6306ecdb-5297-4917-ad9f-c2bf17eae342', 'type': 'incident_events'}]}, 'action_items': {'data': []}, 'custom_field_selections': {'data': []}, 'feedbacks': {'data': []}, 'attachments': {'data': []}, 'slack_messages': {'data': []}}}}}}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from openai import OpenAI\n",
    "from agentsjson.core.executor import execute_flows\n",
    "client = OpenAI(api_key=OPENAI_API_KEY)\n",
    "\n",
    "response = client.chat.completions.create(\n",
    "    model=\"gpt-4o\",\n",
    "    messages=[\n",
    "        {\"role\": \"system\", \"content\": system_prompt},\n",
    "        {\"role\": \"user\", \"content\": query}\n",
    "    ],\n",
    "    tools=core.flows_tools(flows, format=ToolFormat.OPENAI),\n",
    "    temperature=0\n",
    ")\n",
    "\n",
    "response = execute_flows(response, format=core.ToolFormat.OPENAI, bundle=data, flows=flows, auth=auth)\n",
    "\n",
    "response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
